package org.yw.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.yw.dto.Dto;
import org.yw.pojo.DmLinkUser;
import org.yw.pojo.DmUser;
import org.yw.service.LoginService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created By YU WEI on 2020.09.07
 */
@RestController
public class LoginController {
    @Resource
    private LoginService loginService;

    /**
     * 用户登陆
     * @param phone
     * @param password
     * @return
     */
    @PostMapping("login")
    public Dto doLogin(@RequestParam("phone")String phone,
                          @RequestParam("password")String password){
        Dto dto=null;
        try {
            dto=loginService.doLogin(phone,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dto;
    }

    /**
     * 注册用户
     * @param
     * @return
     */
    @PostMapping("register")
    public Dto register(@RequestParam("phone")String phone,
                        @RequestParam("password")String password,
                        @RequestParam("vcode")String vcode){
        Dto dto=null;
        try {
            dto=loginService.register(phone,password,vcode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dto;
    }

    /**
     * 验证phone是否可用
     * @param phone
     * @return
     */
    @PostMapping("checkemail")
    public Dto checkPhone(@RequestParam("phone")String phone){
        Dto dto=null;
        try {
            dto=loginService.checkPhone(phone);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dto;
    }

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @PostMapping("sendcode")
    public Dto sendCode(@RequestParam("phone")String phone){
        Dto dto=null;
        try {
            dto=loginService.sendCode(phone);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dto;
    }

    /**
     * 根据token查询个人用户信息
     * @param request
     * @return
     */
    @PostMapping("queryUserInfoByToken")
    public Dto queryUserInfoByToken(HttpServletRequest request){
        String token=request.getHeader("token");
        Dto dto=null;
        try {
            dto=loginService.queryUserInfoByToken(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dto;
    }

    /**
     * 更新用户个人信息
     * @param user
     * @return
     */
    @PostMapping("modifyUserInfo")
    public Dto modifyUserInfo(@RequestBody DmUser user){
        Dto dto=null;
        try {
            dto=loginService.modifyUserInfo(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dto;
    }

    /**
     * 查询用户常用购票人
     * @param request
     * @return
     */
    @PostMapping("queryLinkUser")
    public Dto queryLinkUser(HttpServletRequest request){
        String token=request.getHeader("token");
        Dto dto=null;
        try {
            dto=loginService.queryLinkUser(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dto;
    }

    /**
     * 增加常用购票人
     * @param request
     * @param name
     * @param idCard
     * @param cardType
     * @return
     */
    @PostMapping("addLinkUser")
    public Dto addLinkUser(HttpServletRequest request,
                           @RequestParam("name")String name,
                           @RequestParam("idCard")String idCard,
                           @RequestParam("cardType")String cardType){
        String token=request.getHeader("token");
        DmLinkUser user=new DmLinkUser();
        user.setName(name);
        user.setIdCard(idCard);
        user.setCardType(Long.parseLong(cardType));
        Dto dto=null;
        try {
            dto=loginService.addLinkUser(token,user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dto;

    }

    /**
     * 验证常用购票人是否存在
     * @param idCard
     * @return
     */
    @PostMapping("queryLinkUserByIdCard")
    public Dto queryLinkUserByIdCard(@RequestParam("idCard")String idCard,
                                     HttpServletRequest request){
        String token=request.getHeader("token");
        Dto dto=null;
        try {
            dto=loginService.queryLinkUserByIdCard(token,idCard);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dto;
    }

    /**
     * 删除常用购票人
     * @param request
     * @param linkId
     * @return
     */
    @PostMapping("deleteLinkUserById")
    public Dto deleteLinkUserById(HttpServletRequest request,
                                  @RequestParam("linkId")Long linkId ){
        String token=request.getHeader("token");
        Dto dto=null;
        try {
            dto=loginService.deleteLinkUserById(token,linkId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dto;
    }

    /**
     * token置换
     * @param request
     * @return
     */
    @PostMapping("replaceToken")
    public Dto replaceToken(HttpServletRequest request){
        String token=request.getHeader("token");
        Dto dto=null;
        try {
            dto=loginService.replaceToken(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dto;
    }
}
